/*
 * @(#)RulesHandler.java  2.0 11/01/11
 * 
 */
package com.hidromedsa.measure.controller.templateprocessor;

import com.hidromedsa.measure.entities.AbstractRule;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

/**
 * <p>Class that manage the marshalling and urmashalling using xml</p> 
 * @version 2.0, 11/01/11
 * @since   JDK1.0
 */
public class RulesHandler {

    // Export
    public static void marshal(List<AbstractRule> rules, File selectedFile, String frontLine, String endLine, String separator)
            throws IOException, JAXBException {
        JAXBContext context;
        BufferedWriter writer = null;
        writer = new BufferedWriter(new FileWriter(selectedFile));
        context = JAXBContext.newInstance(RulesStructure.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
        m.marshal(new RulesStructure(rules,frontLine,endLine,separator), writer);
        writer.close();
    }

    // Import
    public static RulesStructure unmarshal(File importFile) throws JAXBException {
        RulesStructure rules = new RulesStructure();
        JAXBContext context = JAXBContext.newInstance(RulesStructure.class);
        Unmarshaller um = context.createUnmarshaller();       
        rules = (RulesStructure) um.unmarshal(importFile);

        return rules;
    }
}
